/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EJB.Entity;

import Entity.Usuario;
import Entity.Reserva;
import javax.ejb.Stateless;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author mquezada
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> implements UsuarioFacadeLocal {
    @PersistenceContext(unitName = "Webpizza-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }

    public Usuario findByUsername(String username){
        Query q = em.createQuery("SELECT object(u) FROM Usuario AS u"+
                 " WHERE u.username = :username");
        q.setParameter("username",username);
        List<Usuario> resultados = q.getResultList();
        if (resultados == null) return null;
        else if (resultados.size() != 1) return null;
        else return resultados.get(0);
    }

    public List<Reserva> allReservas(int user_id){
        Query q = em.createNamedQuery("SELECT object(r) FROM Reservas AS r"+
                " WHERE r.user_id = :id");
        q.setParameter("user_id", user_id);
        List<Reserva> resultados = q.getResultList();
        return resultados;
    }

}
